package com.ruoyi.project.qyxy;

import com.ruoyi.project.qyxy.enterprise.domain.EnterpriseInfo;
import com.ruoyi.project.qyxy.enterprise.service.IEnterpriseInfoService;
import com.ruoyi.project.qyxy.jlscore.service.IJlOrgScoreService;
import com.ruoyi.project.qyxy.jlscoredtl.service.IJlOrgScoreDetailService;
import com.ruoyi.project.qyxy.person.domain.ZzPerson;
import com.ruoyi.project.qyxy.person.service.IPersonService;
import com.ruoyi.project.qyxy.qua.domain.JlPersonTemp;
import com.ruoyi.project.qyxy.qua.domain.JlQuaTemp;
import com.ruoyi.project.qyxy.qua.domain.JlScoreTemp;
import com.ruoyi.project.qyxy.qua.domain.QuaInfo;
import com.ruoyi.project.qyxy.qua.service.IQuaInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class JlEnterprise {

    //企业信息
    @Autowired
    private IEnterpriseInfoService enterpriseInfoService;

    //资质信息
    @Autowired
    private IQuaInfoService quaInfoService;

    //人员信息
    @Autowired
    private IPersonService personService;

    @Autowired
    private IJlOrgScoreService jlOrgScoreService;

//    @Scheduled(cron = "0 19 1 * * ?")
    public String test1(){
        EnterpriseInfo enterpriseInfo = new EnterpriseInfo();
        enterpriseInfo.setQuaType("5");
        List<EnterpriseInfo> enterpriseList = enterpriseInfoService.selectEnterpriseInfoList(enterpriseInfo);
        for (EnterpriseInfo enterprise : enterpriseList) {
            System.out.println();
            System.out.println();
            System.out.println();
            System.out.println("*************************************************************************");
            System.out.println("企业名称:"+enterprise.getOrgName()+",代码证号:"+enterprise.getBizlicenNum());
            //查询企业注册造价工程师
            List<ZzPerson> zjPersonList = personService.getJlPersonList(enterprise.getBizlicenNum(),"91");
            //查询企业一级注册建造师
            List<ZzPerson> jzPersonList = personService.getJlPersonList(enterprise.getBizlicenNum(),"81");
            //查询企业一级注册建筑师
            List<ZzPerson> jzhPersonList = personService.getJlPersonList(enterprise.getBizlicenNum(),"21");
            //查询企业一级注册结构工程师
            List<ZzPerson> jgPersonList = personService.getJlPersonList(enterprise.getBizlicenNum(),"111");
            //查询企业注册监理工程师
            List<ZzPerson> jlPersonList = personService.getJlPersonList(enterprise.getBizlicenNum(),"71");
            JlPersonTemp jlPersonTemp = new JlPersonTemp(zjPersonList, jzPersonList, jzhPersonList, jgPersonList, jlPersonList);
            jlPersonTemp.printOrgPersonLog();
            //查询企业资质
            QuaInfo quaInfo = new QuaInfo();
            quaInfo.setQuaType("5");
            quaInfo.setOrgCode(enterprise.getBizlicenNum());
            List<QuaInfo> quaInfoList = quaInfoService.selectJlQuaInfoList(quaInfo);
            List<JlQuaTemp> quaTempList = new ArrayList<>();
            formatQuaTemp(quaInfoList,quaTempList,jlPersonTemp);
            //处理合格企业
            for (JlQuaTemp jlQuaTemp : quaTempList) {
                Boolean qualified = jlQuaTemp.isQualified();
                if(qualified){
                    jlQuaTemp.printLog();
                }
            }
            //处理不合格企业
            JlPersonTemp.isDo = JlPersonTemp.DO;
            for (JlQuaTemp jlQuaTemp : quaTempList) {
                Boolean qualified = jlQuaTemp.isQualified();
                if(!qualified){
                    jlQuaTemp.printLog();
                }
            }
            JlPersonTemp.isDo = JlPersonTemp.NOT_DO;
            //计算企业分数
            JlScoreTemp jlScoreTemp = new JlScoreTemp(enterprise, quaTempList);
            jlScoreTemp.computeScore();
            jlOrgScoreService.insertDetail(enterprise,jlScoreTemp,quaTempList);
        }
        return "";
    }

    public void formatQuaTemp(List<QuaInfo> quaInfoList,List<JlQuaTemp> quaTempList,JlPersonTemp jlPersonTemp){
        for (QuaInfo quaInfo : quaInfoList) {
            JlQuaTemp jlQuaTemp = new JlQuaTemp();
            jlQuaTemp.setQuaInfo(quaInfo);
            jlQuaTemp.setJlPersonTemp(jlPersonTemp);
            quaTempList.add(jlQuaTemp);
        }
    }
}
